Helpful Information
 
 
Category: HTML Array
HTML Array -> Perl Array

Hopefully this is a simple question that I am just unable to find an answer to:
I want to take an array of values from an HTML form:

<input name="appItems[]" value="1">
<input name="appItems[]" value="2">
and turn appItems[] into @appItems to be accessed by a Perl script.
So far, I've got this:

@appItems = param('appItems');
but I'm sure it takes some sort of extra step.
Any thoughts? Thanks in advance!

In your <input> tags, you'll need to put a 'type' parameter. Judging by what you have posted it will be a checkbox type.

If you are using CGI.pm's param method, then it should put each of the (checked) checkbox options into an array. There shouldn't be any more required.

Thanks much!

Perl isn't PHP. You don't have to give your HTML form elements special names to get them to come through as an array.

Here's a complete script that demonstrates how to pass arrays via similarly named form parameters. View source to see the HTML bit when you run it. You don't have to use CGI.pm to generate your form values, but if you spend some time to pick it up you'll end up loving it.



#!/usr/bin/perl
use CGI;
my $q=CGI->new();
print $q->header();

if($q->param){
my @numbers=$q->param('numbers');
if(@numbers){
print $q->p('You submitted: '.join(', ',@numbers));
}
}
my @numbers=(1..10);
print $q->start_form().
$q->checkbox_group(-name=>'numbers',-values=>\@numbers,-linebreak=>'true').
$q->submit.
$q->end_form;










privacy (GDPR)